<?php
/**
 * Created by PhpStorm.
 * User: qufan
 * Date: 2016/12/15 0015
 * Time: 23:59
 */

namespace Home\Controller;


class PlayVideoController extends HomeController
{
    public function index(){
        $this->meta_title = '视频查看';
        $this->display();
    }

    public function test(){
        $this->display();
    }
    /**
    * add : 时间段搜索
    * add : 上下序排序
    * user: 边个懂
    * time: 20170522
    **/
    public function playbackList($limit, $offset,$searchOption,$sortOrder, $search = null, $is_open = -1, $begin_time = null,$end_time = null){
        if($is_open!= -1){
            $map['vp.is_open'] = $is_open;
        }
        $fields = "vp.*, vrt.position,o.org_name";
        if(!empty($this->uids)){
            $map['_string'] = "vp.user_id in(".implode(',', $this->uids).")";
        }
        $map['vp.state'] = 1;
        $map['vp.is_center'] = 0;
        if(!empty($search)){
            if($searchOption == 1){
                $map['o.org_name'] = array('like', "%{$search}%");
            }elseif ($searchOption == 2){
                $map['vp.user_name'] = array('like', "%{$search}%");
            }
        }
        empty($begin_time) && $begin_time = '1970-01-01 00:00:00';
        empty($end_time) && $end_time = time_format(NOW_TIME);
        if(empty($end_time)){
            $end_time = time_format(NOW_TIME);
        }else{
            $end_time = date('Y-m-d', strtotime("+1day", strtotime($end_time)));
        }
        $map['vp.add_time'] = array('between', array($begin_time,$end_time));
        
        $order = 'vp.add_time '.$sortOrder;

        $record = M('VideoPlayback')->alias('vp')->join('video_real_time vrt on vp.user_id=vrt.user_id and vp.live_name=vrt.live_name')
            ->join('user u on vp.user_id=u.user_id')->join('organizations o on u.org_id=o.org_id')
            ->field($fields)->where($map)->order($order)->limit($limit, $offset)->select();

        $total_count = M('VideoPlayback')->alias('vp')->join('video_real_time vrt on vp.user_id=vrt.user_id and vp.live_name=vrt.live_name')
            ->join('user u on vp.user_id=u.user_id')->join('organizations o on u.org_id=o.org_id')
            ->where($map)->count();
        $result['rows'] = $record;
        $result['total'] = $total_count;
        $this->ajaxReturn($result);
    }

    public function monitorList($limit, $offset){
        if(!empty($this->uids)){
            $map['_string'] = "orgcode in(".implode(',', $this->codes).")";
        }
        $map['state'] = 1;
        $record = M('VideoMonitor')->where($map)->limit($offset, $limit)->select();
        $total_count = M('VideoMonitor')->where($map)->count();
        $result['rows'] = $record;
        $result['total'] = $total_count;
        $this->ajaxReturn($result);
    }

    public function delete(){
        $ids = I('selectedIds');
        if(!empty($this->uids)){
            $map['_string'] = "user_id in(".implode(',', $this->uids).")";
        }
        $where['playback_id'] = array('in', $ids);
        $res = M('VideoPlayback')->where($where)->setField('state', 0);
        if($res !== false){
            logOperation('视频查看','删除视频回看');
            $this->sucessJson();
        }else{
            $this->errorJson('删除失败');
        }
    }

    public function deleteMonitor(){
        $ids = I('selectedIds');
        if(!empty($this->uids)){
            $where['_string'] = "orgcode in(".implode(',', $this->codes).")";
        }
        $where['video_monitor_id'] = array('in', $ids);
        $res = M('VideoMonitor')->where($where)->setField('state', 0);
        if($res !== false){
            logOperation('视频查看','删除视频监控');
            $this->sucessJson();
        }else{
            $this->errorJson('删除失败');
        }
    }

    public function addMonitor(){
        $model = D('VideoMonitor');
        $_POST['orgcode'] = $this->orgcode;
        if($model->create()){
            $id = $model->add();
            if($id > 0){
                logOperation('视频查看','新增视频监控');
                $this->sucessJson($id);
            }
        }
        $this->errorJson('添加失败');
    }

    /**
    * add : 修改权限操作
    * user: 边个懂
    * time: 20170522
    **/
    public function changeOpen($id, $val){
        $where['playback_id'] = $id;
        $res = M('VideoPlayback')->where($where)->setField('is_open', $val);
        if($res !== false){
            logOperation('历史视频','修改视频开放权限');
            $this->sucessJson();
        }else{
            $this->errorJson('修改失败');
        }
    }



}